package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

/**
 * 反编译一个类的constructor。
 */
public class ReflectTest11 {
    public static void main(String[] args) throws Exception {
        StringBuffer stringBuffer = new StringBuffer();
//        Class<?> vipClass = Class.forName("bean.Vip");
        Class<?> vipClass = Class.forName("java.lang.String");
//        stringBuffer.append("public class Vip { ");
        // + 拼接字符串效率低，追加效率高一些。
        stringBuffer.append(Modifier.toString(vipClass.getModifiers()));
        stringBuffer.append(" class ");
        stringBuffer.append(vipClass.getSimpleName());
        stringBuffer.append("{\n");
        Constructor<?>[] declaredConstructors = vipClass.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            stringBuffer.append("\t");
            stringBuffer.append(Modifier.toString(declaredConstructor.getModifiers()));
            stringBuffer.append(" ");
            stringBuffer.append(vipClass.getSimpleName());
            stringBuffer.append("(");
            // 拼接参数
            Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
            for (Class<?> parameterType : parameterTypes) {
                stringBuffer.append(parameterType.getSimpleName());
                stringBuffer.append(",");
            }
            // 删除最后下标位置上的字符,
            if (parameterTypes.length > 0) {
                stringBuffer.deleteCharAt(stringBuffer.length() - 1);
            }
            stringBuffer.append("){}\n");
        }
        stringBuffer.append("}");
        System.out.println(stringBuffer);
    }
}
